<template>

  <el-table :data="tableDataMarky" style="width: 100%"  >
    <el-table-column prop="id" label="编号" width="80"/>
    <el-table-column prop="name" label="优惠券名称" width="80"/>
    <el-table-column prop="point" label="积分兑换"/>
    <el-table-column label="类型" #default="scope">
      <span>{{ scope.row.useType == 0 ? "通用" : "专用" }}</span>
    </el-table-column>
    <el-table-column label="类型" #default="scope">
      <span>{{ scope.row.overlay == 0 ? "叠加" : "不叠加" }}</span>
    </el-table-column>
    <el-table-column #default="scope" label="有效日期" width="80">
      <span>{{ scope.row.condition.period }}天</span>
    </el-table-column>
    <el-table-column #default="scope" label="领券次数" width="80">
      <span>{{ scope.row.times }}次</span>
    </el-table-column>
    <el-table-column prop="chargeStationInfo.stationName" label="专属优惠券"/>
    <el-table-column #default="scope" label="优惠类型">
      <span>{{ getStatusText(scope.row.condition.type) }}</span>
    </el-table-column>
    <el-table-column #default="scope" label="优惠力度">
      <span>满{{ scope.row.condition.condition }}</span>
    </el-table-column>
    <el-table-column #default="scope" label="优惠方式">
      <span v-if="scope.row.condition.faceValue>=1">减</span>
      <span>{{ scope.row.condition.faceValue }}</span>
      <span v-if="scope.row.condition.faceValue>=1">元</span>
      <span v-if="scope.row.condition.faceValue<1">折扣</span>
    </el-table-column>
    <el-table-column prop="condition.createTime" label="积分兑换"/>
    <el-table-column label="操作" >
      <template #default="scope">
        <el-button @click="ChargeMarkeyDel(scope.row)">删除</el-button>
        <el-button @click="handleRowClick(scope.row)">修改</el-button>

      </template>

    </el-table-column>

  </el-table>

  <el-dialog v-model="dialogTableVisible" title="优惠券新增" width="800">
    <el-form :model="Markeyform" label-width="auto" style="max-width: 600px">
      <el-form-item label="优惠券名称">
        <el-input v-model="Markeyform.name" />
      </el-form-item>
      <el-form-item label="所需积分兑换">
        <el-input-number v-model="Markeyform.point"  />
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="Markeyform.overlay" placeholder="选择状态">
          <el-option label="叠加" :value="0"></el-option>
          <el-option label="非叠加" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设置可领取次数">
        <el-input-number v-model="Markeyform.times" :min="1"  />
      </el-form-item>
      <el-form-item label="是否选择专属充电站">
        <el-select
            v-model="Markeyform.chargeStationInfoId"
            placeholder="请选择"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in chargeInfo"
              :label="item.stationName"
              :key="item.stationName"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设置优惠券有效期">

        <el-input-number v-model="Markeyform.period" :min="1" :max="100" />

      </el-form-item>
      <el-form-item label="满多少元">
        <el-input-number v-model="Markeyform.condition" :min="1"  />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="Markeyform.type" placeholder="选择状态">
          <el-option label="满减" :value="0"></el-option>
          <el-option label="折扣" :value="1"></el-option>
          <el-option label="直减" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="输入金额">
        <el-input-number v-if="Markeyform.type==0" v-model="Markeyform.faceValue"/>
        <el-input-number v-if="Markeyform.type==1" v-model="Markeyform.faceValue" :max="0.9"/>
        <el-input-number v-if="Markeyform.type==2" v-model="Markeyform.faceValue" />
        <el-input-number v-if="Markeyform.type==null" v-model="Markeyform.faceValue"/>
      </el-form-item>

    </el-form>
    <el-button @click="ChargeMarkeySave">确认添加</el-button>

  </el-dialog>
  <el-button @click="ChargeMarkeyAdd">添加</el-button>




  <el-dialog v-model="dialogTableVisibleUp" title="优惠券修改" width="800">
    <el-form :model="Markeyform" label-width="auto" style="max-width: 600px">
      <el-form-item label="优惠券名称">
        <el-input v-model="Markeyform.name" />
      </el-form-item>
      <el-form-item label="所需积分兑换">
        <el-input-number v-model="Markeyform.point"  />
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="Markeyform.overlay" placeholder="选择状态">
          <el-option label="叠加" :value="0"></el-option>
          <el-option label="非叠加" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设置可领取次数">
        <el-input-number v-model="Markeyform.times" :min="1"  />
      </el-form-item>
      <el-form-item label="是否选择专属充电站">
        <el-select
            v-model="Markeyform.chargeStationInfoId"
            placeholder="请选择"
            size="large"
            style="width: 240px"
        >
          <el-option
              v-for="item in chargeInfo"
              :label="item.stationName"
              :key="item.stationName"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设置优惠券有效期">

        <el-input-number v-model="Markeyform.period" :min="1" :max="100" />

      </el-form-item>
      <el-form-item label="满多少元">
        <el-input-number v-model="Markeyform.condition.condition" :min="1"  />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="Markeyform.condition.type" placeholder="选择状态">
          <el-option label="满减" :value="0"></el-option>
          <el-option label="折扣" :value="1"></el-option>
          <el-option label="直减" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="输入金额">
        <el-input-number v-if="Markeyform.condition.type==0" v-model="Markeyform.condition.faceValue"/>
        <el-input-number v-if="Markeyform.condition.type==1" v-model="Markeyform.condition.faceValue" :max="0.9"/>
        <el-input-number v-if="Markeyform.condition.type==2" v-model="Markeyform.condition.faceValue" />
        <el-input-number v-if="Markeyform.condition.type==null" v-model="Markeyform.condition.faceValue"/>
      </el-form-item>

    </el-form>
    <el-button @click="ChargeMarkeyUpdate">确认修改</el-button>

  </el-dialog>
</template>
<script setup>

import {
  GetAdminMarkeDel,
  GetAdminMarkeUpdate,
  GetAdminMarkeyAdd,
  GetAdminMarkeyAll,
  GetAdminMarkeyConditionAll,
  GetAdminMarkeyCoreAll
} from "@/api/markey/markey.js";
import {ref} from "vue";
import {ElMessage, ElMessageBox} from 'element-plus'
import {GetAdminCoreDel} from "@/api/core/core.js";

const dialogTableVisible = ref(false)
const dialogTableVisibleUp=ref(false)
const chargeInfo= ref({})
const chargeCondition= ref({})
const chargeMarkeyDto = ref({
  pageNum: 1,
  pageSize: 10,
})
const tableDataMarky = ref([])

const Markeyform=ref({
})

const getStatusText = (status) => {
  const statusMap = {
    0: "满减",
    1: "折扣",
    2: "直减",
  };
  return statusMap[status] || "未知状态"; // 如果状态未找到，默认返回"未知状态"
};

//查询
function GetAdminMarkeySelect() {
  GetAdminMarkeyAll(chargeMarkeyDto.value).then(res => {
    console.log(res)
    if (res.data.code == 200000) {
      tableDataMarky.value = res.data.data.records
    }
  })
}

GetAdminMarkeySelect()
//优惠券添加
function ChargeMarkeyAdd() {
  console.log("添加优惠券")

  GetAdminMarkeyCoreAll().then(res => {
    chargeInfo.value=res.data
    GetAdminMarkeyConditionAll().then(res => {
      chargeCondition.value =res.data
      console.log(res.data)

      dialogTableVisible.value = true
    })
  })
}

//新增

function ChargeMarkeySave() {
  console.log(Markeyform.value)
  GetAdminMarkeyAdd(Markeyform.value).then(res => {
    console.log(res)
    if (res.data.code == 200000) {
      ElMessage.success('添加成功')
      GetAdminMarkeySelect()
      dialogTableVisible.value = false
    }
  })
}
//修改
function handleRowClick(row){
  console.log( row)
  GetAdminMarkeyCoreAll().then(res => {
    chargeInfo.value=res.data
    GetAdminMarkeyConditionAll().then(res => {
      chargeCondition.value =res.data
      console.log(res.data)
      Markeyform.value=row

      dialogTableVisibleUp.value=true
    })
  })
}
const chargeAdminCouponDto=ref({
conditionId:'',
id:'',
name:"",
overlay:'',
period:'',
chargeStationInfoId:'',
point:'',
times:'',
condition:'',
faceValue:'',
type:'',
})
//修改
function ChargeMarkeyUpdate(){
  chargeAdminCouponDto.value.chargeStationInfoId=Markeyform.value.chargeStationInfoId
  chargeAdminCouponDto.value.conditionId=Markeyform.value.conditionId
  chargeAdminCouponDto.value.id=Markeyform.value.id
  chargeAdminCouponDto.value.name=Markeyform.value.name
  chargeAdminCouponDto.value.overlay=Markeyform.value.overlay
  chargeAdminCouponDto.value.period=Markeyform.value.period
  chargeAdminCouponDto.value.point=Markeyform.value.point
  chargeAdminCouponDto.value.times=Markeyform.value.times
  chargeAdminCouponDto.value.condition=Markeyform.value.condition.condition
  chargeAdminCouponDto.value.faceValue=Markeyform.value.condition.faceValue
  chargeAdminCouponDto.value.type=Markeyform.value.condition.type
  console.log(chargeAdminCouponDto.value)
  console.log(Markeyform.value)
  GetAdminMarkeUpdate(chargeAdminCouponDto.value).then(res => {
    console.log(res)
    if (res.data.code == 200000) {
      ElMessage.success('修改成功')
      GetAdminMarkeySelect()
      dialogTableVisibleUp.value = false
    }
  })
}
const del=ref({
  id:'',
  conditionId:'',
})

//删除操作
function ChargeMarkeyDel(row){
  console.log(row)
  ElMessageBox.confirm(
      '是否确认删除',
      '提醒', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(()=>{
    del.value.id=row.id
    del.value.conditionId=row.conditionId
    console.log(del.value)

    GetAdminMarkeDel(del.value).then(res => {
      if (res.data.code == 200000) {
        ElMessage.success('删除成功')
        GetAdminMarkeySelect()
      }
    })

  })
}
</script>